package blt.app_core.api.car;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import blt.app_core.api.base.bean.OAuth;
import blt.app_core.api.base.bean.State;
import blt.app_core.api.base.callback.ComBack;
import blt.app_core.api.base.callback.StateCallback;
import blt.app_core.api.car.bean.Car;
import blt.app_core.api.car.bean.CarListVo;
import blt.app_core.api.impl.BaseImpl;
import blt.app_core.log.Logger;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by xgp on 2017/6/20.
 */

public class CarImpl extends BaseImpl<CarService> implements CarApi {


    @Override
    public void getCarDetailInfo(int id) {
        builderLoginRequest();
        String carId = String.valueOf(id);
        if (id == 0) {
            carId = null;
        }

        mService.getCarDetailInfo(carId).enqueue(new StateCallback<Car>() {
            @Override
            protected void onSuccess(Car data) {

                notifyClients(CarApi.class, CarApi.onUpdataHomeCar, data);
            }
        });
    }

    @Override
    public void deleteCar(int carId) {
        mService.deleteCar(carId).enqueue(new Callback<OAuth>() {
            @Override
            public void onResponse(Call<OAuth> call, Response<OAuth> response) {

            }

            @Override
            public void onFailure(Call<OAuth> call, Throwable t) {

            }
        });
    }


    @Override
    public void updateCar(String carId, String carPlateNumber, String carModel, int carType, int carSeat, String carPicFront, String carPicLicense) {
        builderLoginRequest();
        Map<String, Object> params = new HashMap<>();
        params.put("carId", carId);
        params.put("carPlateNumber", carPlateNumber);
        params.put("carModel", carModel);
        params.put("carType", carType);
        params.put("carPicFront", carPicFront);
        params.put("carPicLicense", carPicLicense);
        mService.updateCar(carId, carPlateNumber, carModel,
                carType, carSeat, carPicFront, carPicLicense).enqueue(new ComBack<Car>() {
            @Override
            protected void onSuccess(List<Car> data) {
                notifyClients(CarApi.class, CarApi.onUploadCar);
            }
        });

    }


    @Override
    public void getCarList(int page, int pageSize) {
        builderLoginRequest();
        mService.getCarList(page, pageSize).enqueue(new ComBack<Car>() {
            @Override
            protected void onSuccess(List<Car> data) {
                if (data != null) {
                    notifyClients(CarApi.class, CarApi.onCarList, data);
                }
            }
        });

    }

    @Override
    public void setDefaultCar(int carId) {
        mService.setDefaultCar(carId).enqueue(new Callback<State>() {
            @Override
            public void onResponse(Call<State> call, Response<State> response) {

            }

            @Override
            public void onFailure(Call<State> call, Throwable t) {

            }
        });
    }
}
